package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.TxnNumMapMapper;
import cc.rengu.oltp.service.common.entity.TxnNumMap;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易配置表操作方法实现类
 * Created by 王成 on 2017/11/22.
 */
public class TxnNumMapMapperImpl implements TxnNumMapMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public TxnNumMapMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public List<TxnNumMap> loadTxnNumMapInfo(String dbPoolCacheNodeName) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCacheNodeName, AppParamConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadTxnNumMapInfo");
        int pageIndex = 0;
        List<TxnNumMap> txnNumMapList = null;
        while (true) {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex++);
            pageInfo.setPageSize(DbsUtil.MAX_FETCH_NUM);
            PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, new TxnNumMap(), TxnNumMap.class, pageInfo);
            if (null != pageResult) {
                if (null == txnNumMapList) {
                    txnNumMapList = new ArrayList<>();
                }
                txnNumMapList.addAll(pageResult.getResult().stream().map(item -> (TxnNumMap) (item)).collect(Collectors.toList()));
            } else {
                rglog.warn("获取交易映射关系配置信息失败,dbPoolName:<{}>", dbPoolName);
                break;
            }
            if (pageIndex >= pageInfo.getTotalPage()) {
                break;
            }
        }
        return txnNumMapList;
    }

    @Override
    public List<TxnNumMap> selectTxnNumMapInfoByMsgTransChannelId(String instId, String msgSrcId, String transChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnNumMapInfoByMsgTransChannelId");
        TxnNumMap txnNumMap = new TxnNumMap();
        txnNumMap.setInstId(instId);
        txnNumMap.setMsgSrcId(msgSrcId);
        txnNumMap.setTransChannelId(transChannelId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, txnNumMap, TxnNumMap.class);
        if (objectList.isEmpty()) {
            rglog.warn("获取交易映射关系配置信息失败,instId:<{}>,msgSrcId:<{}>,transChannelId:<{}>", instId, msgSrcId, transChannelId);
            return null;
        }
        return objectList.stream().map(item -> (TxnNumMap) (item)).collect(Collectors.toList());
    }

    @Override
    public TxnNumMap selectTxnNumMapInfoByPrimaryKey(String instId, String msgSrcId, String transChannelId, String srcTxnNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnNumMapInfoByPrimaryKey");
        TxnNumMap txnNumMap = new TxnNumMap();
        txnNumMap.setInstId(instId);
        txnNumMap.setMsgSrcId(msgSrcId);
        txnNumMap.setTransChannelId(transChannelId);
        txnNumMap.setSrcTxnNum(srcTxnNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnNumMap, txnNumMap);
        if (0 != iReturnCode) {
            rglog.warn("获取交易映射关系配置信息失败,instId:<{}>,primaryKey:<{}>,iReturnCode:<{}>", instId, msgSrcId, transChannelId, srcTxnNum, iReturnCode);
            return null;
        }
        return txnNumMap;
    }

    @Override
    public int updateTxnNumMapInfoByPrimaryKey(TxnNumMap txnNumMap) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTxnNumMapInfoByPrimaryKey");
        txnNumMap.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnNumMap, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新交易码映射关系失败,instId:<{}>,msgSrcId:<{}>,transChannelId:<{}>,srcTxnNum:<{}>,iReturnCode:<{}>",
                    txnNumMap.getInstId(), txnNumMap.getMsgSrcId(), txnNumMap.getTransChannelId(), txnNumMap.getSrcTxnNum(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int insertTxnNumMapInfo(TxnNumMap txnNumMap) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertTxnNumMapInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnNumMap, null);
        if (0 != iReturnCode) {
            rglog.error("登记交易码映射关系失败,instId:<{}>,msgSrcId:<{}>,transChannelId:<{}>,srcTxnNum:<{}>,iReturnCode:<{}>",
                    txnNumMap.getInstId(), txnNumMap.getMsgSrcId(), txnNumMap.getTransChannelId(), txnNumMap.getSrcTxnNum(), iReturnCode);
        }
        return iReturnCode;
    }
}
